package com.fr.plugin.provider.controller;

import com.fr.base.ServerConfig;
import com.fr.base.TemplateUtils;
import com.fr.decision.webservice.annotation.LoginStatusChecker;
import com.fr.decision.webservice.exception.general.SpecialCharProhibitException;
import com.fr.decision.webservice.utils.WebServiceUtils;
import com.fr.gen.TextGenerator;
import com.fr.general.IOUtils;
import com.fr.invoke.ClassFactory;
import com.fr.invoke.Reflect;
import com.fr.stable.StringUtils;
import com.fr.third.springframework.stereotype.Controller;
import com.fr.third.springframework.web.bind.annotation.RequestMapping;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import com.fr.third.springframework.web.bind.annotation.RequestParam;
import com.fr.third.springframework.web.bind.annotation.ResponseBody;
import com.fr.web.ResourceType;
import com.fr.web.cache.ResourceCache;
import com.fr.web.controller.common.FileContentModificator;
import com.fr.web.controller.common.ResourceUtils;
import com.fr.web.controller.plugin.PluginFileContentModificator;
import com.fr.web.core.gzip.GZIPResponseWrapper;
import com.fr.web.file.FileContentProcessorFactory;
import com.fr.web.struct.category.FileType;
import com.fr.web.struct.category.ParserType;
import com.fr.web.utils.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;

/***
 * @desc:
 * @author: zhuosh
 * @date: 2022-05-04
 */
@RequestMapping(value = "/resfile")
@Controller("fileRequestService")
@LoginStatusChecker(
        required = false
)
public class ResourceController {

    private static final FileContentModificator[] fileContentModificators = new FileContentModificator[]{new PluginFileContentModificator()};

    public ResourceController() {
    }

    @RequestMapping(
            method = {RequestMethod.GET}
    )
    @ResponseBody
    @ResourceCache
    public void getFile(HttpServletRequest req, HttpServletResponse res, @RequestParam("path") String path, @RequestParam(value = "type",required = false) String type, @RequestParam(value = "parser",required = false) String parser) throws Exception {
        if (!WebServiceUtils.containSQLChars(path) && !WebServiceUtils.containSQLChars(type) && !WebServiceUtils.containSQLChars(parser) && !WebServiceUtils.invalidFilePath(path)) {
            GZIPResponseWrapper wrappedResponse = WebServiceUtils.getGZIPResponseWrapper(req, (HttpServletResponse)res);
            if (wrappedResponse != null) {
                res = wrappedResponse;
            }
            if(StringUtils.isEmpty(type)){
                type = "plain";
            }
            if(StringUtils.isEmpty(parser)){
                parser = "plain";
            }
            try {
                FileType fileType = FileType.parse(type);
                PrintWriter writer;
                if (fileType == FileType.PLAIN) {
                    ResourceType resourceType = WebUtils.setResourceContentType(path, (HttpServletResponse)res);
                    if (resourceType == ResourceType.FILE) {
                        InputStream in = ResourceUtils.readResourceByWeb(path);
                        if (in != null) {
                            ParserType parserType = ParserType.parse(parser);
                            writer = WebUtils.createPrintWriter((HttpServletResponse)res);
                            String content;
                            if (parserType == ParserType.DYNAMIC) {
                                content = TemplateUtils.render(IOUtils.inputStream2String(in, "UTF-8"));
                            } else {
                                content = IOUtils.inputStream2String(in, "UTF-8");
                            }

                            writer.write(this.dealWithFileContentModificator(content, req, path));
                            writer.flush();
                            writer.close();
                        }
                    }
                } else if (fileType == FileType.CLASS) {
                    String charset = ServerConfig.getInstance().getServerCharset();
                    TextGenerator generator = (TextGenerator) Reflect.on(ClassFactory.getInstance().classForName(path)).create().get();
                    ((HttpServletResponse)res).setContentType(generator.mimeType() + ";charset=" + charset);
                    String text = generator.text(req, (HttpServletResponse)res);
                    writer = WebUtils.createPrintWriter((HttpServletResponse)res);
                    writer.write(text);
                    writer.flush();
                    writer.close();
                }
            } catch (Exception var16) {
            } finally {
                if (wrappedResponse != null) {
                    wrappedResponse.finishResponse();
                }

            }

        } else {
            throw new SpecialCharProhibitException();
        }
    }

    private String dealWithFileContentModificator(String content, HttpServletRequest req, String path) {
        String modifiedContent = FileContentProcessorFactory.getInstance().modify(content, path, req);
        FileContentModificator[] var5 = fileContentModificators;
        int var6 = var5.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            FileContentModificator modificator = var5[var7];
            if (modificator.accept(req, path)) {
                return modificator.modify(modifiedContent, req, path);
            }
        }

        return modifiedContent;
    }



}
